<template>
    <div class="p-15 p-t-0">
        <div class="table-padding orderDialog">
            <el-form :model="modelForm" ref="modelForm" >
                <div>
                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item prop="warehouse" :rules="[]">
                                <div class="el-input el-input-group el-input-group--prepend el-input--medium">
                                    <div class="el-input-group__prepend">盘点仓库</div>
                                    <el-select v-model="modelForm.warehouseId" placeholder="请选择盘点仓库"
                                               class="w-100p"
                                               :disabled="isEdit"
                                               @change="warehouseChange"
                                               >
                                      <!---->
                                        <el-option
                                                v-for="item in warehouses"
                                                :key="item.id"
                                                :label="item.name"
                                                :disabled="item.state && item.state.key!='NORMAL'"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" >
                          <el-form-item  prop="supplier" :rules="[]" >
                            <div class="el-input el-input-group el-input-group--prepend">
                              <div class="el-input-group__prepend">商品信息库</div>
                              <el-select v-model="modelForm.storeId"   @change="warehouseChange" filterable placeholder="请选择商品信息库" :disabled="isEdit" style="width: 100%">
                                <el-option
                                  v-for="item in storeList"
                                  :key="item.id"
                                  :label="item.name"
                                  :disabled="item.state!='NORMAL'"
                                  :value="item.id">
                                </el-option>
                              </el-select>
                            </div>
                          </el-form-item>

                        </el-col>
                        <el-col :span="8" >
                            <el-form-item prop="no" :rules="[]">
                                <div class="el-input el-input-group el-input-group--prepend el-input--medium">
                                    <div class="el-input-group__prepend">盘点日期</div>
                                     <el-date-picker
                                            v-model="modelForm.createAt"
                                            type="date"
                                            class="w-100p"
                                            :disabled="isEdit"
                                            placeholder="选择日期"
                                            :readonly="true"
                                           >
                                    </el-date-picker>
                                </div>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row :gutter="24">
                        <el-col :span="8">
                            <el-form-item  prop="notMode" >
                                <!--<div class="addInput">-->
                                    <!--<span class="m-r-10">单据类型</span>-->
                                    <!--<el-radio-group v-model="notMode">-->
                                        <!--<el-radio label="STORE">按库存处理</el-radio>-->
                                        <!--<el-radio label="ZORE">按零处理</el-radio>-->
                                    <!--</el-radio-group>-->
                                <!--</div>-->
                              <div class="el-input el-input-group el-input-group--prepend addprepend el-input--medium">
                                <div class="el-input-group__prepend addPrependText-medium" tabIndex="-1">
                                  <span>未盘处理</span>
                                </div>
                                <div style=" text-align:left;padding: 0px 20px; width:100%; border:0.5px solid #c4c4c4;line-height: 30px;">
                                  <!--<el-radio-group  v-model="modelForm.notMode">-->
                                    <!--<el-radio v-for="item in notMode" :label="item.key" :value="item.key" :key="item.key">-->
                                      <!--{{item.text}}</el-radio>-->
                                    <!--&lt;!&ndash;<el-radio label="OTHERIN" >其他入库</el-radio>&ndash;&gt;-->
                                  <!--</el-radio-group>-->
                                  <!--&lt;!&ndash;<el-radio-group v-model="modelForm.notMode" :disabled="isEdit">&ndash;&gt;-->
                                    <!--&lt;!&ndash;<el-radio :label="i.key" v-for="i in notMode">{{i.text}}</el-radio>&ndash;&gt;-->
                                  <!--&lt;!&ndash;</el-radio-group>&ndash;&gt;-->
                                  <el-radio-group v-model="modelForm.notMode" :disabled="isEdit">
                                  <el-radio v-for="item in notMode" :label="item.key" :value="item.key" :key="item.key">
                                    {{item.text}}</el-radio>
                                  </el-radio-group>
                                </div>
                              </div>
                                <!--<div class="addInput" style="display: flex; flex-direction: row;padding: 0;width: 100%">-->
                                    <!--<el-button tabIndex="-1" style="text-align: center;height:39px !important;" >未盘处理</el-button>-->
                                    <!--<div style="padding: 0px 20px; width:100%; border:0.5px solid #c4c4c4;border-left: 0px;">-->
                                        <!--<el-radio-group v-model="notMode" :disabled="isEdit">-->
                                            <!--<el-radio label="STORE">按库存处理</el-radio>-->
                                            <!--<el-radio label="ZORE">按零处理</el-radio>-->
                                        <!--</el-radio-group>-->
                                    <!--</div>-->
                                <!--</div>-->
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item prop="ptype" :rules="[]">
                            <div class="el-input el-input-group el-input-group--prepend el-input--medium" >
                              <div class="el-input-group__prepend">盘点类型</div>
                              <el-select v-model="modelForm.type" placeholder="请选择盘点类型"
                                         class="w-100p"
                                         :disabled="isEdit"
                                         @change="warehouseChange"
                              >
                                <!---->
                                <el-option
                                  v-for="item in types"
                                  :key="item.key"
                                  :label="item.text"
                                  :value="item.key">
                                </el-option>
                              </el-select>
                            </div>
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item  prop="no" :rules="[]">
                                <el-input type="text" v-model="modelForm.remark" size="medium"
                                          class="w-100p" :disabled="isEdit"
                                          auto-complete="off" placeholder="请输入备注信息">
                                    <template slot="prepend">
                                        <el-button>备注</el-button>
                                    </template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div style="color:red;text-align:left">注：请选择盘点库存和盘点类型后再选择原料</div>
                <div class="orderTable" style="padding-top:10px">
                    <el-form-item class="text-left text-left1">
                        <el-button type="primary" size="medium" @click="submitForm('modelForm')"><i class="icon icon-baocun"></i> 保存</el-button>
                        <el-button type="success" size="medium" @click="resetForm('modelForm')"><i class="icon icon-tijiao"></i> 提交并审核</el-button>
                        <el-button type="warning" size="medium" v-if="modelForm.id"><i class="icon icon-dayin"></i> 打印</el-button>
                        <el-button  type="danger" size="medium" @click="goBack()"><i class="icon icon-fanhui1"></i> 取消</el-button>
                    </el-form-item>
                    <el-form-item class="text-left text-left5">
                        <el-button size="medium" type="warning"><i class="icon icon-dayin"></i> 打印</el-button>
                        <el-button size="medium"  type="danger" @click="goBack()"><i class="icon icon-fanhui1"></i> 取消</el-button>
                    </el-form-item>

                    <el-table
                            :data="tableData"
                            border
                            show-summary
                            :summary-method="getSummaries"
                            style="width: 100%">
                        <el-table-column
                                type="index"
                                width="50">
                        </el-table-column>
                      <el-table-column
                        width="80">
                        <template  slot-scope="scope">
                          <span class="el-icon-plus" style="cursor: pointer;font-size: 10px;padding-right: 5px" @click="addColumn(scope.$index)"></span>
                          <span class="el-icon-minus" style="cursor: pointer;font-size: 10px;padding-left: 5px" @click="minusColumn(scope.$index)"></span>
                        </template>
                      </el-table-column>
                      <el-table-column
                        prop="code"
                        label="编号"
                        width="150">
                        <template  slot-scope="scope">
                          <div style="position: relative">
                            <!--<el-select v-model="scope.row.code" :disabled="isEdit" remote @change="codeChange(scope.$index)"   :remote-method="remoteMethod"
                                       filterable="" placeholder="请输入编号" default-first-option @keyup.enter.native="codeEnter($event,scope.$index)">
                              <el-option v-for="(item,index) in material" :disabled="(item.state && item.state.key!='NORMAL')" :key="index"
                                         :label="item.code" :value="item.code">
                                <div style="width:500px;border-bottom:1px solid #c0c0c0;padding:5px 0px">
                                  {{item.value}}
                                </div>
                              </el-option>
                            </el-select>-->

                            <el-select class="codeNo" :class="'coderef_'+scope.$index"
                                       :ref="'coderef_'+scope.$index" v-model="scope.row.code" :disabled="isEdit" remote @change="codeChange(scope.$index)"
                                       :remote-method="remoteMethod"  :loading="loading"  @focus="codeFocus" filterable=""
                                       placeholder="编号/条码/名称/简码" @keyup.enter.native="codeEnter($event,scope.$index)">
                              <el-option :disabled="true" value="0000" style="background-color: #409EFF;color: #fff;">
                                <div style="width:600px;" >
                                  <el-row>
                                    <el-col :span="4">编号</el-col>
                                    <el-col :span="7">条码</el-col>
                                    <el-col :span="7">名称</el-col>
                                    <el-col :span="3">库存单位</el-col>
                                    <el-col :span="3">库存数量</el-col>
                                  </el-row>
                                </div>
                              </el-option>
                              <el-option v-for="(item,index) in material" :disabled="(item.state && item.state!='NORMAL')" :key="index"
                                         :label="item.material_no" :value="item.material_no" >
                                <div style="width:600px;border-bottom:1px solid #c0c0c0;padding:5px 0px">
                                  <!--{{item.value}}[{{item.purchaseUnit}}]-->
                                  <el-row>
                                    <el-col :span="4"><span v-if="item.material_no">{{item.material_no}}</span><span v-else>-</span></el-col>
                                    <el-col :span="7"><span v-if="item.goodcode">{{item.goodcode}}</span><span v-else>-</span></el-col>
                                    <el-col :span="7"><span v-if="item.matrial_name">{{item.matrial_name}}</span><span v-else>-</span></el-col>
                                    <el-col :span="3"><span v-if="item.material_unit">{{item.material_unit}}</span><span v-else>-</span></el-col>
                                    <el-col :span="3"><span v-if="item.count">{{item.count}}</span><span v-else>-</span></el-col>
                                  </el-row>
                                </div>
                              </el-option>
                            </el-select>
                            <span class="el-icon-more" @click="handleIconClick(scope.$index)"
                                  style="cursor: pointer;font-size: 10px;padding-right: 5px; position: absolute;right: 10px;top: 40%"></span>
                          </div>
                        </template>
                      </el-table-column>
                      <el-table-column
                        prop="goodcode"
                        label="条码">
                        <template  slot-scope="scope">
                          <el-tooltip class="item" effect="light" placement="bottom-start" v-if="scope.row.goodcodes && scope.row.goodcodes.length>1">
                            <div slot="content">所选品项含有多个条码:<br/>
                              <div v-for="(item,index) in scope.row.goodcodes">
                                <span>{{index+1}}、{{item}}</span>
                              </div>
                            </div>
                            <el-button  type="text" style="padding-top: 0; padding-bottom: 0">{{scope.row.goodcode}}</el-button>
                          </el-tooltip>
                          <span v-else>{{scope.row.goodcode}}</span><br />
                        </template>
                      </el-table-column>
                        <el-table-column
                                prop="name"
                                label="名称"
                                >
                        </el-table-column>
                        <el-table-column
                                prop="spec"
                                label="规格型号"
                                width="100">
                        </el-table-column>
                        <el-table-column prop="unit" label="单位"  width="80"></el-table-column>
                        <el-table-column prop="stockCount" label="库存数量" >
                            <template  slot-scope="scope">
                                <span>{{scope.row.stockCount | money}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="count" label="实盘数量"  width="100">
                            <template  slot-scope="scope">
                                <el-input v-model="scope.row.count"
                                          :disabled="isEdit" @focus="printNum" @blur="priceValidate(scope.row,'count',scope.$index)"
                                          :class="'focus_input_'+scope.$index"
                                          @keyup.enter.native="nextFocus(scope.$index,'remark_focus_')"
                                          placeholder="0.00"></el-input>
                            </template>
                        </el-table-column>

                        <el-table-column prop="price" label="单价" width="100">
                            <template  slot-scope="scope">
                                {{scope.row.price | money}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="money" label="实盘金额" width="100">
                            <template  slot-scope="scope">
                                <span v-if="scope.row.count==''">0.00</span>
                                <span v-if="scope.row.count!=''">{{scope.row.count*scope.row.price | money}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="diffCount" label="差异数量" width="100">
                            <template  slot-scope="scope">
                                <span v-if="scope.row.count==''">0.00</span>
                                <span v-if="scope.row.count!=''">{{scope.row.count-scope.row.stockCount | money}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="diffMoney" label="差异金额" width="100">
                            <template  slot-scope="scope">
                                <span v-if="scope.row.count==''">0.00</span>
                                <span v-if="scope.row.count!=''">{{(scope.row.count-scope.row.stockCount)*scope.row.price | money}}</span>
                            </template>
                        </el-table-column>

                        <el-table-column prop="biaoshi" label="盘点标识" width="100"></el-table-column>
                        <el-table-column prop="memo" label="备注">
                            <template  slot-scope="scope">
                                <el-input v-model="scope.row.memo" :class="'remark_focus_'+scope.$index" @keyup.enter.native="nextFocus(scope.$index,'')" :disabled="isEdit" placeholder=""></el-input>
                            </template>
                        </el-table-column>
                    </el-table>

                </div>
            </el-form>
        </div>
      <materials-select :isShow="dialogTableVisible" :storeId="modelForm.storeId" :warehouseId="modelForm.warehouseId" :pandian="true" :inventoryType ="modelForm.type" v-on:pushData="getData" v-on:closeDialog="dialogTableVisible=false"></materials-select>
    </div>
</template>
<script>
    //注意路径
    import materialsSelect from "@/components/outMaterialsSelect.vue"
    export default{
        data(){
            return {
              type:"",
                dialogTableVisible:false,
                isEdit:false,
                tableData: [
                  {  id:'',
                    code: '',
                    name: '',
                    goodsSpec: '',
                    purchaseSpec: '',
                    unitId:'',
                    unitName:'',
                    count:'',
                    price:'0.00',
                    money:'0.00',
                    stockUnitName:"",
                    stockCount:"0",
                    stockCost:"0.00",
                    memo:'',}
                ],
                modelForm: {
                    notMode:"",
                    id:"",
                    createAt:""
                },
                oldId:"",
                isPrice:false,
                // pageSizeSet:window.pagination.pageSizeSet,
                // pageSize:window.pagination.pageSize,
                pageTotal:10,
                material:[],
                updateZhong:0,
                types:[],
                content:"",
                warehouse:"",
                warehouses:[],
                restaurants: [],
                timeout:  null,
                gridData: [ ],
                itemActive:0,
                data: [],
              notMode:[],
                selectRow:[],
                inputText:"",
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
              storeList:[],
              query:"",
              loading:false,
            };
        },
      components: {
        materialsSelect
      },
        created(){
            window.this = this;
            this.initData();
        },
        mounted(){

            if(this.$route.query.type!=null && this.$route.query.type!=""){
                if(this.$route.query.type=="5"){//5 说明是查看
                    $("input").attr("disabled", true);
                    $(".text-left1").css("display","none");
                    $(".text-left5").css("display","block");
                    this.isEdit=true;
                    this.isPrice=true;
                }

            }
        },
        methods: {
          printNum(){
            event.currentTarget.select();
          },

          nextFocus(index,name){
            if(name){
              $("."+name+index+" input").focus();
            }else {
              $(".coderef_"+(index+1)+" input").focus();

            }
          },

          codeEnter(e,index){
            console.log(">>>>>>>>>>>>>>>>codeEnter>");
            console.log(e);
            if(e && e.target && e.target.value){
              this.$axios.get("materials/materials/getMaterialsForPandian?page.pn=0&page.size=15&storeId="+this.modelForm.storeId+"&keywords="+e.target.value+"&warehouseId="+this.modelForm.warehouseId).then(data=>{
                this.loading = false;
                if(data.status==200){
                  this.material=data.content.materials;
                  this.$refs['coderef_'+index].blur();
                  if(e && this.material.length<=0){
                    e.target.select();
                    return;
                  }
                  this.codeForTable(index);
                  if(!this.tableData[index+1]){
                    this.addColumn(index);
                  }
                  this.material=[];
                  setTimeout(function () {
                    $(".focus_input_"+index+" input").focus();
                  },300);
                }
              });
            }else {
              return;
            }
          },
          codeForTable(index) {
            console.log("fffffffffffffffff");
            if(this.material && this.material.length>0){
              if(this.tableData && this.tableData.length>0){
                for (var m = 0; m < this.tableData.length; m++) {
                  if (m!=index && this.material[0].material_id == this.tableData[m].id) {
                    this.$message.info("您本次添加的["+this.tableData[m].code+"]品项含有重复");
                    break;
                  }
                }
              }
              console.log("<<<<<<<<<<>>>>>>>");
              console.log(this.material);
              this.tableData[index].id = this.material[0].material_id;
              this.tableData[index].name = this.material[0].matrial_name;
              this.tableData[index].code = this.material[0].material_no;
              this.tableData[index].spec = this.material[0].goods_spec;
              this.tableData[index].unitName = this.material[0].material_unit;
              this.tableData[index].stockCount = this.material[0].count;
              this.tableData[index].amount = this.material[0].amount;
              this.tableData[index].cost = this.material[0].cost;
              this.tableData[index].chuKuPrice=0.00;
              this.tableData[index].goodcode = this.material[0].goodcode;
              this.tableData[index].goodcodes = this.material[0].goodcodes;
              this.tableData[index].goodcodestrs = this.material[0].codes;
            }
            this.material=[];
          },
          codeChange(index) {
            console.log(this.material);
            for (var i = 0; i < this.material.length; i++) {
              if (this.material[i].material_no === this.tableData[index].code) {
                for (var m = 0; m < this.tableData.length; m++) {
                  if (this.material[i].material_id == this.tableData[m].id) {
                    this.$message.info("本次添加含有重复原料");
                    break;
                  }
                }
                this.tableData[index].id = this.material[i].material_id;
                this.tableData[index].name = this.material[i].matrial_name;
                this.tableData[index].code = this.material[i].material_no;
                this.tableData[index].spec = this.material[i].goods_spec;
                this.tableData[index].unit = this.material[i].material_unit;
                this.tableData[index].stockCount = this.material[i].count;
                this.tableData[index].amount = this.material[i].amount;
                this.tableData[index].price = this.material[i].cost;
                this.tableData[index].goodcode = this.material[i].goodcode;
                this.tableData[index].goodcodes = this.material[i].goodcodes;
                this.tableData[index].goodcodestrs = this.material[i].codes;
                this.material = [];
                break;
              }

            }
          },
          remoteMethod(query) {
            if(this.modelForm.warehouseId=="" || this.modelForm.warehouseId==null ){
              this.$message.error("请先选择仓库");
              return;
            }
            if(this.modelForm.type=="" || this.modelForm.type==null ){
              this.$message.error("请先选择盘点类型");
              return;
            }
            if(!this.modelForm.storeId){
              this.$message({
                message: '请选择商品信息库',
                type: 'warning'
              });
              return;
            }
            if (query != '') {
              var search="";
              if(this.modelForm.warehouseId){
                search="&warehouseId="+this.modelForm.warehouseId+"&"+search;
              }
              search="&pandian=true"+"&"+search;
              if(this.modelForm.type){    //盘点类型
                search="&inventoryType="+this.modelForm.type+"&"+search;
              }
              this.loading = true;

              this.$axios.get("materials/materials/getMaterialsForPandian??page.pn=0&page.size=15&storeId="+this.modelForm.storeId+"&keywords="+query+search).then(data=>{
                this.loading = false;
                if(data.status==200){
                  this.material=data.content.materials;
                 /* var a=0;
                  var tempData=data.content.materials;
                  if(data.content.materials && data.content.materials.length<0){
                    this.message.error("暂未搜索到数据");
                    return;
                  }
                  for(var j=0;j<tempData.length;j++){
                    tempData[j].value="["+tempData[j].material_no+"]"+tempData[j].matrial_name;
                  }
                  if(data.content.materials && data.content.materials.length>0){
                    for(let i=0;i<data.content.materials.length;i++){
                      a=0;
                      if(this.material && this.tableData && this.tableData.length>0 && this.tableData[0].id){
                        for(let s=0;s<this.material.length;s++){
                          if( this.material[s].material_id==data.content.materials[i].material_id ){
                            a++;
                          }
                        }
                        if(a<=0){
                          if(this.tableData[i].id){
                            console.log(this.material,"o");
                            for(var i=0;i<tempData.length;i++){
                              this.material.push(
                                tempData[i]
                              );
                            }

                          }
                        }
                      }else{
                        console.log("L");
                        this.material=tempData;
                      }
                    }
                  }else {
                    console.log("h");
                    this.material=tempData;
                  }*/
                }
              });
            } else {
              this.material=[];
            }
          },
          codeFocus(){
            this.material=[];
          },
          getData(data){
            if(!this.material){
              this.material = [];
            }
            var a=0;
            for(var i=0;i<data.length;i++){
              a=0;

              for(let s=0;s<this.material.length;s++){
                if(this.material[s].id==data[i].material_id){
                  console.log("3");
                  a++;
                }
              }
              if(a<=0){
                console.log("u");
                data[i].value="["+data[i].material_no+"]"+data[i].matrial_name;
                this.material.push(data[i]);
              }
              this.tableData.push({
                id:data[i].material_id,
                code:data[i].material_no,
                name:data[i].matrial_name,
                spec:data[i].goods_spec,
                unit:data[i].material_unit,
                count: '0',
                chuKuPrice:0.00,
                memo: '',
                amount:data[i].amount,
                price:data[i].cost,
                stockCount:data[i].count
              });
            }
            for(var i=0;i<this.tableData.length;i++){
              if(this.tableData[i].id==''){
                this.tableData.splice(i,1);
              }
            }
            this.dialogTableVisible=false;
          },
        //页面第一次进来获取表单需要的数据
            initData(){
                var id=this.$route.query.id;

                var query="";
                if(id!=null && id!=""){
                    query="?id="+id;
                    this.modelForm.id=id;
                    this.updateZhong=2;
                }

                this.$axios.get("stores/inventoryOrders/create"+query).then(data=>{
                    if(data.status==200){
                        var data=data.content;
                      // data.inventoryOrder.notMode=data.notMode[0].key;
                      this.types=data.inventoryType;
                      data.inventoryOrder.notMode=data.notMode[0].key;
                      this.modelForm=data.inventoryOrder;
                      console.log(this.modelForm);
                        this.warehouses=data.warehouses;
                        this.notMode=data.notMode;
                        this.storeList=data.storeList;

                        // this.restaurants=data.material;
                        // this.material=data.material;

                        this.modelForm.createAt=this.moment().format("YYYY-MM-DD");

                        if(id!=null && id!=""){
                            this.tableData=data.detailList;
                            this.material=data.materials;
                            for(var i=0;i<this.tableData.length;i++){
                              for(var j=0;j<this.material.length;j++){
                                if(this.tableData[i].materialId==this.material[j].material_id){
                                  this.tableData[i].stockCount=this.material[j].count;
                                }
                              }
                              this.tableData[i].id=this.tableData[i].materialId;
                              this.tableData[i].goods_spec=this.tableData[i].goodsSpec;
                              this.tableData[i].count=(this.tableData[i].realCount).toFixed(2);
                              this.tableData[i].money=(this.tableData[i].realCount*this.tableData[i].price).toFixed(2);
                              this.tableData[i].diffCount=(this.tableData[i].realCount-this.tableData[i].stockCount).toFixed(2);
                              this.tableData[i].diffMoney=(this.tableData[i].price*this.tableData[i].diffCount).toFixed(2);
                              this.tableData[i].biaoshi = this.tableData[i].mark?"是":"否";

                              console.log(this.tableData[i].code);
                              if(this.tableData[i].code && this.tableData[i].code!=''){
                                console.log("cvvvvvvvvvb>>>>>>>>>>");
                                if(this.tableData[i].code.indexOf(","+this.query+",")!=-1){
                                  this.tableData[i].goodcode = this.query;
                                }else {
                                  this.tableData[i].goodcode = this.tableData[i].code.substring(1,this.tableData[i].codes.indexOf(",",2));
                                }
                                this.tableData[i].code = this.tableData[i].code.substring(1,this.tableData[i].code.length-2);
                                if(this.tableData[i].code!=''){
                                  this.tableData[i].goodcodes = this.tableData[i].code.split(",");
                                }
                              }else{
                                this.tableData[i].goodcode="";
                                this.tableData[i].goodcodes=[];
                              }
                              this.tableData[i].goodcodestrs = this.tableData[i].code;
                              this.tableData[i].code = this.tableData[i].no;
                            }

                        }
                    }else{
                        this.$message.error(data.body.message);
                    }
                })
            },
            warehouseChange(){

             /* if(!this.modelForm.storeId){
                this.$message({
                  message: '请选择商品原料库',
                  type: 'warning'
                });
                return;
              }*/

                if(this.updateZhong>0){
                    this.updateZhong=this.updateZhong-1;
                    return;
                }
                if(this.modelForm.warehouseId==null || this.modelForm.warehouseId==""){
                    return;
                }
                if(this.modelForm.type==null || this.modelForm.type==""){
                    return;
                }
                this.$axios.get("materials/materials/getMaterialsForPandian?warehouseId="+this.modelForm.warehouseId+"&inventoryType="+this.modelForm.type+"&pandian=true"+"&storeId="+this.modelForm.storeId).then(data=> {
                    if (data.status == 200) {

                        if(data.content.materials && data.content.materials.length>0){
                          this.tableData = [];
                          var datas=data.content.materials;
                          for(var i=0;i<datas.length;i++){
                            var item={};
                            item.id=datas[i].material_id;
                            item.code=datas[i].material_no;
                            item.name=datas[i].matrial_name;
                            item.spec=datas[i].goods_spec;
                            item.goodcode=datas[i].goodcode;
                            item.goodcodes=datas[i].goodcodes;
                            item.unit=datas[i].material_unit;
                            item.stockCount = datas[i].count;
                            item.price=(datas[i].cost*1).toFixed(2);
                            item.count='';
                            item.memo='';
                            var biaoshi='否';
                            if( item.count!=''){
                              biaoshi='是'
                            }
                            item.biaoshi=biaoshi;
                            this.tableData.push(item);
                          }
                        }else{
                          this.tableData=[
                            {  id:'',
                              code: '',
                              name: '',
                              goodsSpec: '',
                              purchaseSpec: '',
                              unitId:'',
                              unitName:'',
                              count:'',
                              price:'0.00',
                              money:'0.00',
                              stockUnitName:"",
                              stockCount:"0",
                              stockCost:"0.00",
                              memo:'',}
                          ]
                        }

                    }
                });
            },
            handleIconClick(index){
                if(this.isEdit) {
                   return;
                }
                if(this.modelForm.warehouseId==null || this.modelForm.warehouseId==""){
                    this.$message.error("请选择盘点仓库！");
                    return;
                }
                if(this.modelForm.type==null || this.modelForm.type==""){
                    this.$message.error("请选中盘点类型！");
                    return;
                }
              if(!this.modelForm.storeId){
                this.$message({
                  message: '请选择商品信息库',
                  type: 'warning'
                });
                return;
              }
                this.dialogTableVisible = true;
                this.itemActive = index;

            },
            selectGood(page){
              this.dialogTableVisible = true;
            },
            handleSizeChange(val){
                this.pageSize=val;
               this.selectGood();
            },
            handleNodeClick(data) {
                var id=data.id;
                this.content="search.category.id_eq=" + id+"";
                this.selectGood();
            },
            newLabel(newList){
                if(newList){
                    for(var i=0;i<newList.length;i++){
                        newList[i].label=newList[i].name;
                        if(newList[i].children){
                            this.newLabel(newList[i].children)
                        }
                    }
                }
            },
            goBack() {
                this.router.go(-1);
            },
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    if(index==6 || index==7 ||index==8 || index==9 || index==10){
                        const values = data.map(item => Number(item[column.property]));
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return ((prev*1 + curr*1)*1).toFixed(2);
                            } else {
                                return (prev*1).toFixed(2);
                            }
                        }, 0);

                    }
                });

                return sums;
            },
            submitForm(formName) {
                console.log(this.tableData)
                this.type=1;
                this.validateData(formName);
            },
            resetForm(formName) {
                this.type=2;
                this.validateData(formName);
            },
            validateData(fromName){
                if (this.modelForm.warehouseId == null) {
                    this.$message.error("请选择盘点仓库！");
                    return;
                }
                if (this.tableData == null || this.tableData.length == 0) {
                    this.$message.error("请选择盘点原料！");
                    return;
                }

              for (var i = 0; i < this.tableData.length; i++) {
                if (this.tableData[i].id == "") {
                  if (this.tableData.length == 1) {
                    this.$message.error("请添加单据明细！");
                    return;
                  }
                  this.tableData.splice(i, 1);
                  i--;
                }

              }
              if (this.tableData == null || this.tableData.length == 0) {
                this.$message.error("请添加单据明细！");
                return;
              }

                if(this.$route.query.type=="4") {//5 说明是查看
                    this.modelForm.id="";
                }
                this.$refs[fromName].validate((valid)=> {
                    if (valid) {
                        var data = {};
                        for(var i=0;i<this.warehouses.length;i++){
                          if(this.modelForm.warehouseId==this.warehouses[i].id){
                            this.modelForm.warehouseName=this.warehouses[i].name
                          }
                        }
                        data.type=this.type;
                        data.inventoryOrders = this.modelForm;
                        var tempData=[];
                        for(var i=0;i<this.tableData.length;i++){
                          tempData.push({
                            materialId:this.tableData[i].id,
                            no:this.tableData[i].code,
                            name:this.tableData[i].name,
                            spec:this.tableData[i].spec,
                            unit:this.tableData[i].unit,
                            count:this.tableData[i].stockCount,
                            realCount:this.tableData[i].count,
                            price:this.tableData[i].price,
                            amount:this.tableData[i].count*this.tableData[i].price,
                            mark:this.tableData[i].biaoshi=='是'?true:false,
                            remark:this.tableData[i].memo
                          })
                        }
                        data.details = tempData;
                        this.$axios.post("stores/inventoryOrders/create", data, "json").then(data=>{
                            if (data.status == 200) {
                                this.$message({
                                    message: data.message,
                                    type: 'success'
                                });
                                this.$emit('addSuccess');
                                this.router.push({path:"/inventoryOrders"});
                            } else {
                                this.$message.error(data.message);
                            }
                        })
                    } else {
                        this.$message.error("请检查输入项");
                        return false;
                    }
                });
            },
            priceValidate(item, key,index){
              console.log("-------------ffff");
              console.log(index);

                if(item[key]!=""){
                    var isNum=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
                    if( ! isNum.test(item[key])){
                        item[key]="0.00"
                    }else{
                        item[key]=item[key]
                    }
                }
                switch (key){
                    case "count":
                        if(item.count==""){
                            item.money= "0.00";
                            item.diffCount= "0.00";
                            item.diffMoney= "0.00";
                            item.biaoshi='否';
                        }else{
                            item.money= item.price*item.count;
                            item.diffCount= item.count-item.stockCount;
                            item.diffMoney= ((item.count-item.stockCount)*item.price).toFixed(2);
                            item.biaoshi='是';
                        }
                        this.tableData.splice(index,1,item);
                        break;
                }


//                console.log(this.count)
//                console.log(this.count)

            },
            handleCurrentChange(item){
                this.currentPage3=item;
                this.selectGood(item);
            },
            inputClick(){
                this.content="search.code_eq="+this.inputText;
                this.selectGood("");
            },
            addColumn(index){
                if(this.isEdit) {
                    return;
                }
                this.tableData.splice(index+1,0,{
                    id:'',
                    code: '',
                    name: '',
                    goodsSpec: '',
                    purchaseSpec: '',
                    unitId:'',
                    unit:'',
                    count:'',
                    price:'0.00',
                    money:'0.00',
                    stockUnitName:"",
                    stockCount:"0",
                    stockCost:"0.00",
                    memo:'',
                })
            },
            minusColumn(index){
                if(this.isEdit) {
                    return;
                }
                if(this.tableData.length>1){
                    this.tableData.splice(index,1);
                }else{
                    this.$message.info("最少保留一条");
                }
            }
        },

    }
</script>
<style scoped>
    .el-form-item{
        margin-bottom: 10px !important;
    }
    .el-table tr{
        border-bottom: 1px solid #dfe6ec !important;
    }
    .text-left5{display: none;}
</style>
